package com.yanqu.road.web.timer;

import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.executor.ScheduledThreadPoolHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.web.WebServer;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class GeneralAgentTimerMgr extends TempMgr {

	private static ScheduledExecutorService timerExecutorService = ScheduledThreadPoolHelper.createGameScheduledThreadPoolExecutor(1, "timer-web");

	private static MgrDataSaveTask mgrDataSaveTask;

	private static LoginQueueOverTimeCheckTask loginQueueOverTimeCheckTask;

	private static ActivityMallReloadTask activityMallReloadTask;

	private static LoadRegisterNumFromRedisTask loadRegisterNumFromRedisTask;

	private static WebDataAnalyticsTask webDataAnalyticsTask;

	private static AllActivityReloadTask allActivityReloadTask;

	private static TestJobTask testJobTask;

	private static UploadRoleCreateCountToRedisTask uploadRolceCreateCountToRedisTask;

	@Override
	public boolean reloadData() {
		// 保存log到数据库
		int interval = 60;
		if (mgrDataSaveTask == null) {
			mgrDataSaveTask = new MgrDataSaveTask();
			timerExecutorService.scheduleWithFixedDelay(mgrDataSaveTask, 1, 1, TimeUnit.SECONDS);
		}

		// 所有活动重新加载 10分钟加载一次
		if (allActivityReloadTask == null) {
			allActivityReloadTask = new AllActivityReloadTask();
			timerExecutorService.scheduleWithFixedDelay(allActivityReloadTask, 600, 600, TimeUnit.SECONDS);
		}

		//检测登录队列人数
		interval = 3;
		if(loginQueueOverTimeCheckTask ==null){
			loginQueueOverTimeCheckTask = new LoginQueueOverTimeCheckTask();
			timerExecutorService.scheduleWithFixedDelay(loginQueueOverTimeCheckTask, interval, interval, TimeUnit.SECONDS);
		}

		if(null == activityMallReloadTask){
			activityMallReloadTask = new ActivityMallReloadTask();
			timerExecutorService.scheduleWithFixedDelay(activityMallReloadTask, 3, 1, TimeUnit.SECONDS);
		}

		if (loadRegisterNumFromRedisTask == null) {
			loadRegisterNumFromRedisTask = new LoadRegisterNumFromRedisTask();
			timerExecutorService.scheduleWithFixedDelay(loadRegisterNumFromRedisTask, 0, 30, TimeUnit.SECONDS);
		}

		if (webDataAnalyticsTask == null) {
			webDataAnalyticsTask = new WebDataAnalyticsTask();
			timerExecutorService.scheduleWithFixedDelay(webDataAnalyticsTask, 0, 5, TimeUnit.SECONDS);
		}

		if (testJobTask == null){
			testJobTask = new TestJobTask();
			timerExecutorService.scheduleWithFixedDelay(testJobTask, 0, 1, TimeUnit.SECONDS);
		}

		if(!WebServer.getInstance().isUserCenter()) {
			if (null == uploadRolceCreateCountToRedisTask) {
				uploadRolceCreateCountToRedisTask = new UploadRoleCreateCountToRedisTask();
				timerExecutorService.scheduleWithFixedDelay(uploadRolceCreateCountToRedisTask, 0, 120, TimeUnit.SECONDS);
			}
		}
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	@Override
	public boolean stop() {
		cancelTask(mgrDataSaveTask);
		cancelTask(loginQueueOverTimeCheckTask);
		cancelTask(activityMallReloadTask);
		cancelTask(webDataAnalyticsTask);
		if(testJobTask != null){
			cancelTask(testJobTask);
		}
		cancelTask(uploadRolceCreateCountToRedisTask);
		return false;
	}

	private static void cancelTask(GeneralTask task){
		if(null != task){
			task.cancel(true);
		}
	}
}
